Skip to content

Add KnownImmediateMethods flag to avoid @GuardedBy false positives on immediate invocations of guarded access - #5940

Open
EamonTracey wants to merge 7 commits into
google:masterfrom
EamonTracey:etracey/guarded-by-lambda-smarter
Open

Add KnownImmediateMethods flag to avoid @GuardedBy false positives on immediate invocations of guarded access#5940
EamonTracey wants to merge 7 commits into
google:masterfrom
EamonTracey:etracey/guarded-by-lambda-smarter

Conversation

@EamonTracey

@EamonTracey EamonTracey commented Jul 1, 2026

Copy link
Copy Markdown

Addresses #5938

Adds the GuardedBy:KnownImmediateMethods flag, which marks methods that invoke lambdas / method references / runnables immediately in the caller's lock scope. This allows users to avoid false positive @GuardedBy checks.

For example, with -XepOpt:GuardedBy:KnownImmediateMethods=com.example.Transaction#doSomething:

class Transaction {
  @GuardedBy("this")
  int x;

  public synchronized void handle() {
    doSomething(() -> {
      ++x;  // OK: 'doSomething' is configured to run the lambda immediately.
    });
  }

  private void doSomething(Runnable r) {
    r.run();
  }
}

@google-cla

google-cla Bot commented Jul 1, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@EamonTracey
EamonTracey marked this pull request as ready for review July 1, 2026 23:01
@EamonTracey

Copy link
Copy Markdown
Author

Hey @kluever and @cpovirk 👋! I am seeking a code review for this contribution; are either of you are available to do it? This addresses #5938

@EamonTracey

Copy link
Copy Markdown
Author

Politely bumping @kluever @cpovirk 😃

@cushon

cushon commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

I suggested an alternative in the bug that I’d prefer to an annotation for this: #5938 (comment)

@EamonTracey EamonTracey changed the title Add @RunsImmediately annotation to avoid @GuardedBy false positives on immediate invocations of guarded access Add KnownImmediateMethods flag to avoid @GuardedBy false positives on immediate invocations of guarded access Aug 1, 2026
@EamonTracey

Copy link
Copy Markdown
Author

@cushon Thanks for the suggestion, updated to introduce a new flag rather than annotation. This PR is ready for review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants